<template>
  <el-card>
    <div slot="header" class="clearfix relative">
      <span class="title-icon" style="float:left;">
        询单 &nbsp;&nbsp;&nbsp;&nbsp;{{
          onlineInquirySheetData.onlineInquiryNo
        }}
        <span v-if="onlineInquirySheetData.cuser">
          &nbsp;&nbsp;&nbsp;&nbsp;{{
            userSource[onlineInquirySheetData.cuser]
          }}
          &nbsp;&nbsp;&nbsp;&nbsp;{{ onlineInquirySheetData.cdate }}
        </span>
      </span>
      <el-button
        type="primary"
        size="mini"
        class="header-save-btn"
        @click="submitForm"
        v-show="!disabled"
        v-loading.fullscreen.lock="fullscreenLoading"
        >保存
      </el-button>
    </div>
    <el-form
      ref="onlineInquirySheetForm"
      :model="onlineInquirySheetData"
      :rules="onlineInquirySheetRules"
      size="mini"
      label-width="100px"
    >
      <el-form-item label="物流方法" prop="onlineInquiryShippingWay">
        <el-radio-group
          v-model="onlineInquirySheetData.onlineInquiryShippingWay"
          size="medium"
        >
          <el-radio
            v-for="(item,
            value) in initOnlineInquirySheetSelectData.onlineInquiryShippingWayMap"
            :key="value"
            :label="parseInt(value)"
            >{{ m(item) }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item
        label="预计渠道"
        prop="instockChannelIds"
        v-if="
          onlineInquirySheetData.onlineInquiryShippingWay == 1 ||
            onlineInquirySheetData.onlineInquiryShippingWay == 4
        "
      >
        <el-select
          v-model="onlineInquirySheetData.instockChannelIds"
          placeholder="请选择预计渠道"
          filterable
          multiple
          :style="{ width: '100%' }"
          ref="channelName"
          :disabled="disabled"
        >
          <template
            v-if="initOnlineInquirySheetSelectData.shippingOfChannelNameMap"
          >
            <el-option
              v-for="(item, value) in initOnlineInquirySheetSelectData
                .shippingOfChannelNameMap[
                onlineInquirySheetData.onlineInquiryShippingWay
              ]"
              :key="value"
              :label="item"
              :value="parseInt(value)"
            ></el-option>
          </template>
        </el-select>
      </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item label="品名" prop="declareName">
            <el-input
              v-model="onlineInquirySheetData.declareName"
              placeholder="请输入品名"
              clearable
              :style="{ width: '100%' }"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="件数和尺寸" prop="customerTotalPackages">
            <el-input
              v-model="onlineInquirySheetData.customerTotalPackages"
              placeholder="请输入件数和尺寸"
              clearable
              :style="{ width: '100%' }"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="实重(KG)" prop="customerActualWeight">
            <el-input
              type="number"
              v-model.trim="onlineInquirySheetData.customerActualWeight"
              placeholder="请输入实重(KG)"
              clearable
              :style="{ width: '100%' }"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="体积(CBM)" prop="customerVolume">
            <el-input
              type="number"
              v-model.trim="onlineInquirySheetData.customerVolume"
              placeholder="请输入体积(CBM)"
              clearable
              :style="{ width: '100%' }"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="货型(重量/体积)" prop="cargoType">
            <el-input
              v-model="onlineInquirySheetData.cargoType"
              placeholder="请输入货型(重量/体积)"
              :disabled="true"
              clearable
              :style="{ width: '100%' }"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="货好时间" prop="goodsTime">
            <el-date-picker
              v-model="onlineInquirySheetData.goodsTime"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              :style="{ width: '100%' }"
              placeholder="请选择货好时间"
              clearable
            ></el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item
        label="是否报关"
        prop="isCustomerDeclaration"
        v-show="
          onlineInquirySheetData.onlineInquiryShippingWay == 2 ||
            onlineInquirySheetData.onlineInquiryShippingWay == 3
        "
      >
        <el-radio-group
          v-model="onlineInquirySheetData.isCustomerDeclaration"
          size="medium"
        >
          <el-radio
            v-for="(item,
            value) in initOnlineInquirySheetSelectData.clearanceGoodsTypeMap"
            :key="value"
            :label="parseInt(value)"
            >{{ m(item) }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item
        label="交货方式"
        prop="deliveryType"
        v-show="
          onlineInquirySheetData.onlineInquiryShippingWay == 1 ||
            onlineInquirySheetData.onlineInquiryShippingWay == 4 ||
            onlineInquirySheetData.onlineInquiryShippingWay == 5
        "
      >
        <el-radio-group
          v-model="onlineInquirySheetData.deliveryType"
          size="medium"
        >
          <el-radio
            v-for="(item,
            value) in initOnlineInquirySheetSelectData.deliveryTypeMap"
            :key="value"
            :label="parseInt(value)"
            >{{ m(item) }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item
        label="托盘尺寸"
        prop="palletDimension"
        v-show="onlineInquirySheetData.deliveryType == 2"
      >
        <el-input
          v-model="onlineInquirySheetData.palletDimension"
          placeholder="请输入托盘尺寸"
          clearable
          :style="{ width: '100%' }"
        ></el-input>
      </el-form-item>
      <el-form-item label="提货方式" prop="pickUpType">
        <el-radio-group
          v-model="onlineInquirySheetData.pickUpType"
          size="medium"
        >
          <el-radio
            v-for="(item,
            value) in initOnlineInquirySheetSelectData.pickUpGoodsTypeMap"
            :key="value"
            :label="parseInt(value)"
            >{{ m(item) }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item
        label="提货地址"
        prop="pickUpAddress"
        v-if="onlineInquirySheetData.pickUpType == 1"
      >
        <el-input
          v-model="onlineInquirySheetData.pickUpAddress"
          clearable
          :style="{ width: '100%' }"
        ></el-input>
        <div v-show="!disabled">
          <el-tag
            type="info"
            size="mini"
            v-for="(item,
            value) in initOnlineInquirySheetSelectData.shippingAddressMap"
            :key="value"
            :label="item"
            style="margin-right:10px"
            @click="selectPickUpAddress(item)"
            >{{ item }}
          </el-tag>
        </div>
      </el-form-item>
      <el-form-item
        label="送货仓库"
        prop="storageLocation"
        v-if="onlineInquirySheetData.pickUpType == 0"
      >
        <el-radio-group
          v-model="onlineInquirySheetData.storageLocation"
          size="medium"
        >
          <el-radio
            v-for="(item,
            value) in initOnlineInquirySheetSelectData.storageLocationMap"
            :key="value"
            :label="parseInt(value)"
            >{{ m(item) }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="目的国家" prop="countryId">
        <el-select
          v-model="onlineInquirySheetData.countryId"
          placeholder="请选择目的国家"
          clearable
          filterable
          :style="{ width: '100%' }"
          @change="holdTypeChange"
        >
          <el-option
            v-for="(item,
            value) in initOnlineInquirySheetSelectData.countryCodeAndNameMap"
            :key="value"
            :label="item"
            :value="parseInt(value)"
          ></el-option>
        </el-select>
        <div v-show="!disabled">
          <el-tag
            type="info"
            size="mini"
            v-for="(item,
            value) in initOnlineInquirySheetSelectData.frequentlyUsedCountryMap"
            :key="value"
            :label="item"
            style="margin-right:10px"
            @click="selectCountry(parseInt(value))"
            >{{ item }}
          </el-tag>
        </div>
      </el-form-item>
      <el-form-item
        label="起运港"
        prop="shipmentPort"
        v-if="
          onlineInquirySheetData.onlineInquiryShippingWay == 2 ||
            onlineInquirySheetData.onlineInquiryShippingWay == 3 ||
            onlineInquirySheetData.onlineInquiryShippingWay == 5
        "
      >
        <el-select
          v-model="onlineInquirySheetData.shipmentPort"
          placeholder="请选择起运港"
          clearable
          filterable
          :style="{ width: '100%' }"
        >
          <div
            v-if="
              onlineInquirySheetData.onlineInquiryShippingWay == 2 ||
                onlineInquirySheetData.onlineInquiryShippingWay == 3
            "
          >
            <el-option
              v-for="(item,
              value) in initOnlineInquirySheetSelectData.configPortNameMap"
              :key="value"
              :label="item"
              :value="parseInt(value)"
            ></el-option>
          </div>
          <div v-else-if="onlineInquirySheetData.onlineInquiryShippingWay == 5">
            <el-option
              v-for="(item,
              value) in initOnlineInquirySheetSelectData.configAirPortCodeMap"
              :key="value"
              :label="item"
              :value="parseInt(value)"
            ></el-option>
          </div>
        </el-select>
      </el-form-item>
      <el-form-item
        label="目的港"
        prop="destinationPort"
        v-if="
          onlineInquirySheetData.onlineInquiryShippingWay == 3 ||
            onlineInquirySheetData.onlineInquiryShippingWay == 5
        "
      >
        <el-select
          v-model="onlineInquirySheetData.destinationPort"
          placeholder="请选择目的港"
          clearable
          filterable
          :style="{ width: '100%' }"
        >
          <div v-if="onlineInquirySheetData.onlineInquiryShippingWay == 3">
            <el-option
              v-for="(item,
              value) in initOnlineInquirySheetSelectData.configPortNameMap"
              :key="value"
              :label="item"
              :value="parseInt(value)"
            ></el-option>
          </div>
          <div v-else-if="onlineInquirySheetData.onlineInquiryShippingWay == 5">
            <el-option
              v-for="(item,
              value) in initOnlineInquirySheetSelectData.configAirPortCodeMap"
              :key="value"
              :label="item"
              :value="parseInt(value)"
            ></el-option>
          </div>
        </el-select>
      </el-form-item>
      <template
        v-if="
          onlineInquirySheetData.onlineInquiryShippingWay == 1 ||
            onlineInquirySheetData.onlineInquiryShippingWay == 2 ||
            onlineInquirySheetData.onlineInquiryShippingWay == 4
        "
      >
        <el-form-item label="目的仓库类" prop="warehouseType">
          <el-radio-group
            v-model="onlineInquirySheetData.warehouseType"
            size="medium"
          >
            <el-radio
              v-for="(item,
              value) in initOnlineInquirySheetSelectData.stockTypeMap"
              :key="value"
              :label="value"
              >{{ m(item) }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          label="仓库代码"
          prop="dispatchAddressCode"
          v-if="
            onlineInquirySheetData.warehouseType !=
              initOnlineInquirySheetSelectData.personalAddressCode
          "
        >
          <el-select
            v-model="onlineInquirySheetData.dispatchAddressCode"
            placeholder="请选择仓库代码"
            clearable
            filterable
            :style="{ width: '100%' }"
          >
            <el-option
              v-for="(item, value) in initOnlineInquirySheetSelectData
                .shipingFbastockMap[onlineInquirySheetData.warehouseType]"
              :key="value"
              :label="value + '   ' + item"
              :value="value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-row v-else>
          <el-col :span="12">
            <el-form-item label="邮编" prop="zipCode">
              <el-input
                v-model="onlineInquirySheetData.zipCode"
                placeholder="请输入邮编"
                clearable
                :style="{ width: '100%' }"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="地址" prop="address">
              <el-input
                v-model="onlineInquirySheetData.address"
                placeholder="请输入地址"
                clearable
                :style="{ width: '100%' }"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </template>
      <el-row>
        <el-col :span="12">
          <el-form-item label="客户" prop="customerName">
            <!--<el-input v-model="onlineInquirySheetData.customerName" placeholder="请输入客户" clearable
                      :style="{width: '100%'}"></el-input>-->

            <!-- <el-select v-model="onlineInquirySheetData.customerName" placeholder="请输入客户" clearable filterable remote :remote-method="remoteCustomer"
                       :style="{width: '100%'}" allow-create> -->
            <el-select
              v-model="onlineInquirySheetData.customerName"
              placeholder="请输入客户"
              clearable
              filterable
              remote
              :remote-method="remoteCustomer"
              :style="{ width: '100%' }"
              @blur="selectBlur"
            >
              <el-option
                v-for="item in customerOptions"
                :key="item.id"
                :label="item.text"
                :value="item.text"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="客户目标价" prop="customerTargetPrice">
            <el-input
              v-model="onlineInquirySheetData.customerTargetPrice"
              placeholder="请输入客户目标价"
              clearable
              :style="{ width: '100%' }"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="询单备注" prop="remark">
        <el-input
          v-model="onlineInquirySheetData.remark"
          type="textarea"
          :placeholder="m('onlineinquirysheet_remark')"
          :autosize="{ minRows: 3, maxRows: 3 }"
          :style="{ width: '100%' }"
        ></el-input>
      </el-form-item>
      <el-form-item label="询单目的" prop="enquiryPurpose">
        <el-select
          multiple
          v-model="onlineInquirySheetData.enquiryPurpose"
          placeholder="请选择询单目的"
          clearable
          filterable
          :style="{ width: '100%' }"
        >
          <el-option
            v-for="(item,
            value) in initOnlineInquirySheetSelectData.onlineInquiryPurposeMap"
            :key="value"
            :label="m(item)"
            :value="parseInt(value)"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="上传附件">
        <el-upload
          ref="field118"
          multiple
          action="fakeaction"
          :limit="10"
          :file-list="onlineInquirySheetAccessorys"
          :http-request="uploadFn"
          :on-remove="fileRemove"
          :on-preview="handleFile"
          :before-upload="field118BeforeUpload"
        >
          <el-button size="small" type="default" icon="el-icon-upload"
            >点击上传</el-button
          >
          <div slot="tip" class="el-upload__tip">
            最多只能上传10个文件，且不超过10M
          </div>
        </el-upload>
      </el-form-item>
    </el-form>
  </el-card>
</template>
<script>
//引入 ajax 工具
import { ajax } from "@/store/yg/common/utils";
//翻译工具
import { m } from "@/store/yg/common/local";
import axios from "axios";
import { untransform } from "@/store/yg/common/uri";
import * as constant from "@/store/yg/common/constant";

export default {
  props: {
    onlineInquirySheetDataParent: {
      type: Object,
      default: function() {
        return {};
      }
    },
    copyOnlineSheet: Number,
    editOnlineSheet: Number
  },
  data() {
    return {
      disabled: false,
      fullscreenLoading: false,
      action: "",
      initOnlineInquirySheetSelectData: {
        personalAddressCode: "700",
        shipingFbastockMap: {},
        shippingOfChannelNameMap: {},
        onlineInquiryPurposeMap: {}
      },
      row: {},
      customerOptions: {},
      onlineInquirySheetAccessorys: [],
      onlineInquirySheetData: {
        id: null,
        onlineInquiryNo: "",
        onlineInquiryShippingWay: 1,
        instockChannelIds: [],
        declareName: "",
        customerActualWeight: null,
        customerVolume: null,
        cargoType: null,
        goodsTime: new Date(),
        isCustomerDeclaration: null,
        customerTotalPackages: "",
        deliveryType: 1,
        palletDimension: "",
        pickUpType: "",
        pickUpAddress: "",
        storageLocation: "",
        countryId: null,
        shipmentPort: null,
        destinationPort: null,
        warehouseType: "100",
        dispatchAddressCode: "",
        zipCode: "",
        address: "",
        customerName: "",
        customerTargetPrice: "",
        remark: "",
        enquiryPurpose: [],
        cdate: "",
        accessorys: []
      },
      countryEntity: {
        country: ""
      },
      onlineInquirySheetRules: {
        instockChannelIds: [
          {
            required: true,
            message: "请选择渠道"
          }
        ],
        onlineInquiryShippingWay: [
          {
            required: true,
            message: "物流方法不能为空"
          }
        ],
        declareName: [
          {
            required: true,
            message: "请输入品名"
          }
        ],
        customerActualWeight: [
          {
            required: true,
            message: "请输入实重(KG)",
            pattern: /^\d+(\.\d{1,3})?$/
          }
        ],
        pickUpAddress: [
          {
            required: true,
            message: "提货地址不能为空"
          }
        ],
        storageLocation: [
          {
            required: true,
            message: "送货仓库不能为空"
          }
        ],
        shipmentPort: [
          {
            required: true,
            message: "起运港不能为空"
          }
        ],
        destinationPort: [
          {
            required: true,
            message: "目的港不能为空"
          }
        ],
        customerVolume: [
          {
            required: true,
            message: "请输入体积(CBM)",
            pattern: /^\d+(\.\d{1,3})?$/
          }
        ],

        cargoType: [],
        goodsTime: [
          {
            required: true,
            message: "请选择货好时间"
          }
        ],
        customerTotalPackages: [],
        deliveryType: [
          {
            required: true,
            message: "交货方式不能为空"
          }
        ],
        pickUpType: [
          {
            required: true,
            message: "提货方式不能为空"
          }
        ],
        countryId: [
          {
            required: true,
            message: "请选择目的国家"
          }
        ],
        warehouseType: [
          {
            required: true,
            message: "目的仓库类不能为空"
          }
        ],
        dispatchAddressCode: [
          {
            required: true,
            message: "仓库代码不能为空"
          }
        ],
        enquiryPurpose: [
          {
            required: true,
            message: "询单目的不能为空"
          }
        ],
        zipCode: [
          {
            required: true,
            message: "邮编不能为空"
          }
        ]
      },
      userSource: {},
      attrs: {
        class: "com.tofba.app.client.statistics.StatementExportClient",
        uri: "/statistics/statementCenter/main"
      }
    };
  },
  created: function() {
    // this.action = constant.API_SERVER + "/api/consume/upload?uri=" + untransform(this.$route.params.uri) + "&f=batchUploadFile&type=blank&name=sub";
    this.action =
      constant.API_SERVER +
      "/api/consume/upload?uri=/open/message&f=batchUploadFile&type=blank&class=com.tofba.app.client.onlineInquiry.OnlineInquirySheetClient";
  },
  watch: {
    onlineInquirySheetDataParent: {
      handler(nVal) {
        if (!nVal.accessorys) {
          nVal.accessorys = [];
        } else {
          this.onlineInquirySheetAccessorys = [];
          for (let url of nVal.accessorys) {
            let fileName = this.getAccessFileName(url);
            this.onlineInquirySheetAccessorys.push({
              name: fileName,
              url: url
            });
          }
        }
        this.onlineInquirySheetData = nVal;
        this.disabled = true;
      }
    },
    // 'onlineInquirySheetData.onlineInquiryShippingWay':{
    //   handler(newVal, oldVal) {
    //     if(null == this.onlineInquirySheetData.id){
    //       this.onlineInquirySheetData.instockChannelIds = [];
    //     }
    //   }
    // },
    "onlineInquirySheetData.instockChannelIds": {
      handler(newVal, oldVal) {
        // console.log(this.$refs['channelName']);
        if (newVal && newVal.length > 0 && !this.disabled) {
          if (oldVal.length == 0 && this.onlineInquirySheetData.id != null) {
            //第一次加载，不修改
          } else {
            let channelId = newVal[newVal.length - 1];
            if (
              this.initOnlineInquirySheetSelectData.shippingOfChannelNameMap
            ) {
              const channelMap = this.initOnlineInquirySheetSelectData
                .shippingOfChannelNameMap[
                this.onlineInquirySheetData.onlineInquiryShippingWay
              ];
              if (channelMap) {
                let channelName = channelMap[channelId.toString()];
                for (let item in this.initOnlineInquirySheetSelectData
                  .frequentlyUsedCountryMap) {
                  var countryName = this.initOnlineInquirySheetSelectData
                    .frequentlyUsedCountryMap[item];
                  if (channelName.indexOf(countryName) > -1) {
                    this.onlineInquirySheetData.countryId = parseInt(item);
                    this.holdTypeChange(parseInt(item));
                    return;
                  }
                }
              }
            }
          }
        }
      }
    },
    "onlineInquirySheetData.customerActualWeight": {
      handler(newVal) {
        this.onlineInquirySheetData.cargoType = (
          this.onlineInquirySheetData.customerActualWeight /
          this.onlineInquirySheetData.customerVolume
        ).toFixed(3);
      }
    },
    "onlineInquirySheetData.customerVolume": {
      handler(newVal) {
        this.onlineInquirySheetData.cargoType = (
          this.onlineInquirySheetData.customerActualWeight /
          this.onlineInquirySheetData.customerVolume
        ).toFixed(3);
      }
    }
    // 'onlineInquirySheetData.warehouseType': {
    //   handler(oldVal,newVal) {
    //     if(!this.onlineInquirySheetData.id){
    //       this.onlineInquirySheetData.dispatchAddressCode = "";
    //     }
    //   }
    // }
  },

  mounted() {
    this.init();
    if (
      (this.copyOnlineSheet || this.editOnlineSheet) &&
      this.$route.query.pid
    ) {
      const params = { id: this.$route.query.pid };
      this.queryOnlineInquirySheet(params);
    }
    if (this.$store.state.blank.entry.sources) {
      const userSource = this.$store.state.blank.entry.sources["userSource"];
      if (userSource) {
        this.userSource = userSource;
      }
    }
  },
  methods: {
    async holdTypeChange(val) {
      this.countryEntity.country = val;
      const res = await ajax("findShipingFbaStockById", this.countryEntity);
      this.initOnlineInquirySheetSelectData.shipingFbastockMap = res;
      console.log(this.initOnlineInquirySheetSelectData.shipingFbastockMap);
    },
    selectBlur(e) {
      if (e.target.value) {
        this.onlineInquirySheetData.customerName = e.target.value;
      }
    },
    async queryOnlineInquirySheet(params) {
      const row = await ajax("findOne", params, {
        uri: "/waybill/onlineinquiry/list",
        type: "main"
      });
      for (let key in this.onlineInquirySheetData) {
        const value = row[key];
        if (value) {
          this.onlineInquirySheetData[key] = value;
        }
      }
      this.onlineInquirySheetData.pickUpType = row.pickUpType;
      if (this.copyOnlineSheet) {
        this.onlineInquirySheetData.id = null;
        this.onlineInquirySheetData.onlineInquiryNo = null;
        this.onlineInquirySheetData.accessorys = [];
      }
      if (this.onlineInquirySheetData.accessorys) {
        for (let url of this.onlineInquirySheetData.accessorys) {
          let fileName = this.getAccessFileName(url);
          this.onlineInquirySheetAccessorys.push({ name: fileName, url: url });
        }
      }
    },
    async init() {
      this.initOnlineInquirySheetSelectData = await ajax(
        "initOnlineInquirySheetSelectData",
        {},
        {
          uri: "/waybill/onlineinquiry/list",
          type: "main"
        }
      );
    },
    // async findOnlineInquirySheetAndReply(){
    //     const data = await ajax('findOnlineInquirySheetAndReplyById', {id: this.$route.query.pid},{uri:'/waybill/onlineinquiry/list',type:'main'});
    //     this.onlineInquirySheetData = data;
    //     // const toReplyData = {};
    //     // toReplyData["onlineInquiryShippingWay"] = data.onlineInquiryShippingWay;
    //     // toReplyData["onlineInquiryReplys"] = data.onlineInquiryReplys;
    //     this.$emit('child-event',data);
    // },
    getAccessFileName(accellUrl) {
      let accessoryIndex = accellUrl.lastIndexOf("/");
      let fileName = accellUrl.substring(accessoryIndex + 1);
      fileName = fileName.substring(fileName.indexOf("_") + 1);
      return fileName;
    },
    handleFile(file) {
      // let fileName = this.getAccessFileName(file.url);
      // var path = (path = constant.FILE_SERVER + "/" + file.url);
      // location.href = constant.API_SERVER + "/open/downloadFileByHttp?fileName=" + fileName + "&path=" + path;
      console.log(file.url);
      window.open(constant.FILE_SERVER + "/" + file.url);
    },
    uploadFn(file) {
      // 根据后台需求数据格式
      const form = new FormData();
      // 文件对象
      form.append("items[]", file.file);
      axios({
        method: "post",
        url: this.action,
        data: form
      })
        .then(response => {
          if (response.data.code == 200) {
            const fileList = response.data.data;
            for (let fileUrl of fileList) {
              let fileName = this.getAccessFileName(fileUrl);
              this.onlineInquirySheetAccessorys.push({
                name: fileName,
                url: fileUrl
              });
              this.onlineInquirySheetData.accessorys.push(fileUrl);
            }
          } else {
            this.$notify.error({
              title: "错误",
              message: "上传失败!" + response.data.message
            });
          }
        })
        .catch(error => {
          console.log(error);
          this.$notify.error({
            title: "错误",
            message: "上传失败!"
          });
        });
    },
    fileRemove(file, fileList) {
      this.onlineInquirySheetAccessorys = fileList;
      this.onlineInquirySheetData.accessorys.splice(
        this.onlineInquirySheetData.accessorys.findIndex(
          item => item === file.url
        ),
        1
      );
    },

    submitForm() {
      // TODO 提交表单
      // console.log(this.onlineInquirySheetData);
      this.$refs["onlineInquirySheetForm"].validate(valid => {
        if (!valid) return;
        this.saveOrUpdateOnlineInquirySheet(this.onlineInquirySheetData);
        // const params = this.onlineInquirySheetData;
        // ajax('saveOrUpdateOnlineInquirySheet', params).then(res => {
        //   if (res.code == 200) {
        //     this.$message.success("操作成功");
        //     //1 手动关闭 弹窗 Drawer
        //     this.$store.dispatch('toggleModal');
        //     //2 关闭后手动加载最新数据
        //     this.$store.dispatch('loadPageData', this.$route.params.uri);
        //   } else {
        //     this.$message.error(m(res.message));
        //   }
        // }, error => {
        //   this.$message.error(error);
        // })
      });
    },
    async saveOrUpdateOnlineInquirySheet(params) {
      this.fullscreenLoading = true;
      const res = await ajax("saveOrUpdateOnlineInquirySheet", params, {
        uri: "/waybill/onlineinquiry/list",
        type: "main"
      });
      this.fullscreenLoading = false;
      if (res.code == 200) {
        this.$message.success("操作成功");
        // //1 手动关闭 弹窗 Drawer
        // this.$store.dispatch('toggleModal');
        // //2 关闭后手动加载最新数据
        // this.$store.dispatch('loadPageData', this.$route.params.uri);

        window.open("/#/main/waybill__onlineinquiry__list", "_self");
      } else {
        this.$message.error(m(res.message));
      }
    },
    m(params) {
      return m(params);
    },
    selectPickUpAddress(pickUpAddress) {
      this.onlineInquirySheetData.pickUpAddress = pickUpAddress;
    },
    selectCountry(countryId) {
      this.onlineInquirySheetData.countryId = countryId;
    },
    async remoteCustomer(query) {
      if (query !== "") {
        this.customerOptions = await ajax(
          "searchCustomerSelect",
          { name: query },
          this.attrs
        );
      } else {
        this.customerOptions = {};
      }
    },
    field118BeforeUpload(file) {
      let isRightSize = file.size / 1024 / 1024 < 10;
      if (!isRightSize) {
        this.$message.error("文件大小超过 10MB");
      }
      return isRightSize;
    }
  }
};
</script>
